package GUITOOl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;

/**
 * The type My file lable.
 *
 * @author dzr
 * @version 1.0
 */
public class MyFileLable extends JLabel {
    /**
     * The File name.
     */
    String fileName;
    /**
     * The File path.
     */
    String filePath;
    /**
     * The File.
     */
    File file;

    /**
     * Instantiates a new MyfileLable.
     *
     * @param filePath the file path
     */
    public MyFileLable(String filePath){
        this.filePath = filePath;
        this.file = new File(filePath);
        this.fileName = file.getName();
        this.setBackground(Color.lightGray);
        this.setText("来自朋友的文件" + "\n" + fileName);
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2){
                    try {
                        Desktop.getDesktop().open(file.getParentFile());
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                }
            }
        });
    }
}
